/*
 * BanBu_PluginViewController
 * @description 本文件提供个人空间的修改设置动态插件界面的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供个人空间的修改设置动态插件界面的ViewController
 */

#import "BanBu_PluginViewController.h"
#import "plugincell.h"
#import "UIImageView+WebCache.h"
#import "AppCommunicationManager.h"
#import "AppDataManager.h"
#import "Profile.h"
#import "AppDataManager.h"
@interface BanBu_PluginViewController ()
{
    NSInteger selectedIndex;
}

@end

@implementation BanBu_PluginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _imageArray = [[NSMutableArray alloc] initWithCapacity:0];
    
    selectedIndex = -1;
    
    [self getVoiceDefault];
    
    [self getSelectIndex];
}

-(void)getSelectIndex
{
    int i = 0;
    for(NSDictionary *image in _imageArray)
    {
        if(self.currentURL&&self.currentURL.length&&[[image valueForKey:@"urlof"] isEqualToString:self.currentURL])
        {
            selectedIndex = i+1;
            return;
        }
        i++;
    }
    selectedIndex = -1;
}


/**
 *	@brief	获取图片列表
 */
- (void)getVoiceDefault
{
    //先判断缓存数据是否过期
    BOOL flag = [BanBu_CommUtils needUpdateCache:PlugTimeKey timeInterval:PlugTimeInterval];
    //已经过期
    if (flag)
    {
        //重新获取数据
        [AppComManager getBanBuData:BanBu_Get_Space_Picture_Sound_List par:[NSDictionary dictionaryWithObjectsAndKeys:@"picture",@"getby", nil] delegate:self];
        return;
    }
    else
    {
        //获取数据源,修改2014/07/16
        NSDictionary * plugDic = [BanBu_CommUtils getLocalData:PlugDataKey];
        [self.imageArray removeAllObjects];
        [self.imageArray addObjectsFromArray:[plugDic valueForKey:@"list"]];
        [self getSelectIndex];
        [self.tableView reloadData];
    }
    
}

/**
 *	@brief	设置图片
 *
 *	@param 	_url 设置的图片url
 */
- (void)setPictureFloat:(NSString *)_url
{
    self.navigationController.view.userInteractionEnabled = NO;
    [AppComManager getBanBuData:BanBu_Set_Spaceset_Byuser par:[NSDictionary dictionaryWithObjectsAndKeys:_url,@"valueof",KEY_PICTURE,@"keyof",nil] delegate:self];
    [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"chuliNotice", nil) activityAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.imageArray.count + 1;
}

-(plugincell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"plugincell";
    plugincell  * cell = (plugincell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell= (plugincell *)[[[NSBundle  mainBundle]  loadNibNamed:@"plugincell" owner:self options:nil]  lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.select.hidden = YES;
    if(indexPath.row == 0)
    {
        cell.txt.text = NSLocalizedString(@"default_none", nil);
    }
    else
    {
        [cell.pho setImageWithURL:[NSURL URLWithString:[[self.imageArray objectAtIndex:indexPath.row - 1] valueForKey:@"urlof"]] placeholderImage:[UIImage imageNamed:@"default.png"] andType:0];
    }
    if(selectedIndex == indexPath.row   )
    {
        cell.select.hidden = NO;
    }

//    NSLog(@"%d",indexPath.row);
//    if(indexPath.row&&self.imageArray.count&&[[[self.imageArray objectAtIndex:indexPath.row - 1] valueForKey:@"urlof"] isEqualToString:@"dfs"])
//    {
//        cell.select.hidden = NO;
//    }

    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return NSLocalizedString(@"default_plug", 默认插件);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        return;
    }
    selectedIndex = indexPath.row;
    [self.tableView reloadData];
    NSLog(@"%d",indexPath.row);
    NSString *url = [[_imageArray objectAtIndex:indexPath.row - 1] valueForKey:@"urlof"];
    [[NSNotificationCenter defaultCenter]postNotificationName:Notification_FloatImage_Change object:url];
    //设置Picture
    [self setPictureFloat:url];
}

-(void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    self.navigationController.view.userInteractionEnabled = YES;
    if(error)
    {
        [TKLoadingView showTkloadingAddedTo:self.view title:NSLocalizedString(@"uploadFailNotice", nil) activityAnimated:NO duration:2.0];
        return;
    }
    else if([AppComManager respondsDic:resDic isFunctionData:BanBu_Set_Spaceset_Byuser])
    {
        [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"modifySucceed", nil) activityAnimated:NO duration:1];
        
        for(BanBu_PersonDataViewController *v in self.navigationController.viewControllers)
        {
            if([v isKindOfClass:[BanBu_PersonDataViewController class]])
            {
                [v getMyInfo];
                break;
            }
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if([AppComManager respondsDic:resDic isFunctionData:BanBu_Get_Space_Picture_Sound_List])
    {
        //设置缓存数据
        [BanBu_CommUtils setLocalData:resDic key:PlugDataKey];
        
        //设置开始的缓存时间
        [BanBu_CommUtils setCacheTime:PlugTimeKey];
        [self.imageArray removeAllObjects];
        [self.imageArray addObjectsFromArray:[resDic valueForKey:@"list"]];
        [self getSelectIndex];
        [self.tableView reloadData];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [AppComManager cancalHandlesForObject:self];
    [_imageArray release],_imageArray = nil;
    [_tableView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setTableView:nil];
    [super viewDidUnload];
}
@end
